package back_0413.lbservice.server;

//import back_041311.busuanzi.util.MIME;
//import static io.netty.handler.codec.http.HttpHeaders.Names.*;
//import static io.netty.handler.codec.http.HttpHeaders.isKeepAlive;

/**
 * Created by bruce on 15/4/4.
 */
public class StaticHanlder {

//    public static void bath(ChannelHandlerContext ctx, HttpRequest request) {
//        try {
//
//            final String uri = request.getUri();
//            final QueryStringDecoder query = new QueryStringDecoder(uri);
//
//            String path = query.path();
//
//            String fileName = path.substring(path.lastIndexOf('/') + 1);
//
//            String mimeType = MIME.mimeTypeByPath(path);
//
//            HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
//            response.headers().set("Transfer-Encoding", "chunked");
//            response.headers().set(CONTENT_TYPE, mimeType);
//
//            if (isKeepAlive(request)) response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
//
//
//            ctx.write(response);
//
//            InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
//            if (in == null) in = Thread.currentThread().getContextClassLoader().getResourceAsStream(path.substring(1));
//
//            ChannelFuture writeFuture = ctx.write(new ChunkedStream(in), ctx.newProgressivePromise());
//
//            ChannelFuture lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
//
//            if (!isKeepAlive(request)) lastContentFuture.addListener(ChannelFutureListener.CLOSE);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }
//
//    public static void handleImg(ChannelHandlerContext ctx, HttpRequest request) {
//        try {
//
//            final String uri = request.getUri();
//            final QueryStringDecoder query = new QueryStringDecoder(uri);
//
//            String path = query.path();
//
//            String fileName = path.substring(path.lastIndexOf('/') + 1);
//
//            String mimeType = MIME.mimeTypeByPath(path);
//
////            File file=new File("src/main/resources/favicon.ico");
////            RandomAccessFile raf = new RandomAccessFile(file, "r");
////            long fileLength = raf.length();
////
////            HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
////            response.headers().set(CONTENT_TYPE, mimeType);
////            if (isKeepAlive(request)) response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
////            response.headers().set(CONTENT_LENGTH,fileLength);
////
////
////            // Write the initial line and the header.
////            ctx.write(response);
////
////            ChannelFuture   sendFileFuture =
////                    ctx.write(new DefaultFileRegion(raf.getChannel(), 0, fileLength), ctx.newProgressivePromise());
////            // Write the end marker.
////            ChannelFuture  lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
////            if (!isKeepAlive(request)) lastContentFuture.addListener(ChannelFutureListener.CLOSE);
////
////            {
////                InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
////                byte[] bytes2 = ByteStreams.toByteArray(in);
////                System.out.println(bytes2.length);
////
////                byte[] bytes = IOUtils.toByteArray(in);
////
//////                StringWriter writer = new StringWriter();
//////                IOUtils.copy(in, writer, "UTF-8");
//////
//////                String theString = writer.toString();
//////
//////                int l=theString.length();
////                ByteBuf content = Unpooled.wrappedBuffer(bytes);
////
////                int l2=content.readableBytes();
////
//////                System.out.println(l);
////                System.out.println(l2);
////
////            }
//            {
//                File file = new File("D:\\gitosc\\busuanzi\\target\\classes\\" + fileName);
//                FileInputStream fs = new FileInputStream(file);
//                System.out.println(fs.available());
//
//                InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
//                System.out.println(in.available());
//
////                ByteBuf content = Unpooled.wrappedBuffer(theString.getBytes(Charsets.UTF_8));
//                HttpResponse response1 = new DefaultFullHttpResponse(HTTP_1_1, OK);
////            response.headers().set("Transfer-Encoding", "chunked");
//                response1.headers().set(CONTENT_TYPE, mimeType);
////            response.headers().set(CONTENT_TYPE, "text/plain");
//                if (isKeepAlive(request)) response1.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
//                response1.headers().set(CONTENT_LENGTH, in.available());
//
//                ctx.write(response1);
//
//                ChannelFuture writeFuture = ctx.write(new ChunkedStream(in), ctx.newProgressivePromise());
//
//                ChannelFuture lastContentFuture1 = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
////
//                if (!isKeepAlive(request)) lastContentFuture1.addListener(ChannelFutureListener.CLOSE);
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }
//
//    public static void main(String[] args) {
//        File f = new File("src/main/resources/favicon.ico");
//        System.out.println(f.length());
//        System.out.println(f.exists());
//    }
}
